//index.js
//获取应用实例
import { dateformat } from '../../utils/dateFormat.js';
var app = getApp();

Page({
  data: {
    mapData: {},
    addDataTrend: {},
    allDataTrend: {},
    allCureTrend: {},
    headerData:{},
    areaDetail:[],
    overseaData: []
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function() {
    var that = this;
    wx.request({
      url: 'https://tianqiapi.com/api?version=epidemic&appid=55745663&appsecret=5GHmLpgC',
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      success: function(res) {
        const headerData = {
          diagnosed:res.data.data.diagnosed,
          diagnosedIncr:res.data.data.diagnosedIncr,
          suspect:res.data.data.suspect,
          suspectIncr:res.data.data.suspectIncr,
          cured:res.data.data.cured,
          curedIncr:res.data.data.curedIncr,
          death:res.data.data.death,
          deathIncr:res.data.data.deathIncr
        };
        var countryData = [];
        var mapssData = res.data.data.area;
        var mapsData = {
         //seriesData:[],
          countryData:[],
        };
        mapssData.forEach(item => {
            var datasMap = {
               name: item.provinceName,
               value: item.confirmedCount,
            }
          mapsData.countryData.unshift(datasMap)
          });
          
        const mapData = {
          seriesData: mapsData.countryData
        }
        that.setData({
          mapData: mapData,
          headerData:headerData
        });
      }
    });
    wx.request({
      url: 'https://ncov.zhhainiao.com/ncov/data/get',
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        var addTrendData = {
          xAxisData: res.data.ncov_data.china_day_add_list.date, // 日期
          confirmedIncr: res.data.ncov_data.china_day_add_list.confirm_day_add, // 新增确诊
          suspectedIncr: res.data.ncov_data.china_day_add_list.suspect_day_add // 新增疑似
        };
        var allTrendData = {
          xAxisData: res.data.ncov_data.china_day_total_list.date, // 日期
          confirmedNum: res.data.ncov_data.china_day_total_list.confirm_total, // 累计确诊
          suspectedNum: res.data.ncov_data.china_day_total_list.suspect_total, // 现有疑似
          deathsNum: res.data.ncov_data.china_day_total_list.dead_total, // 累计死亡
          curesNum: res.data.ncov_data.china_day_total_list.heal_total // 累计治愈
        };
        const addDataTrend = {
          title: '全国疫情每日新增确诊/疑似病例走势',
          color: ["#e83132", "#ec9217"],
          legendData: ['新增确诊', '新增疑似'],
          xAxisData: addTrendData.xAxisData,
          seriesData: [{
            name: '新增确诊',
            type: 'line',
            data: addTrendData.confirmedIncr
          }, {
            name: '新增疑似',
            type: 'line',
            data: addTrendData.suspectedIncr
          }]
        };
        const allDataTrend = {
          title: '全国疫情累计确诊/疑似病例走势',
          color: ["#e83132", "#ec9217"],
          legendData: ['累计确诊', '现有疑似'],
          xAxisData: allTrendData.xAxisData,
          seriesData: [{
            name: '累计确诊',
            type: 'line',
            data: allTrendData.confirmedNum
          }, {
            name: '现有疑似',
            type: 'line',
            data: allTrendData.suspectedNum
          }]
        };
        const allCureTrend = {
          title: '全国疫情累计治愈/死亡病例走势',
          color: ["#2fbd51", "#4d5054"],
          legendData: ['累计治愈', '累计死亡'],
          xAxisData: allTrendData.xAxisData,
          seriesData: [{
            name: '累计治愈',
            type: 'line',
            data: allTrendData.curesNum
          }, {
            name: '累计死亡',
            type: 'line',
            data: allTrendData.deathsNum
          }]
        };
        let _areaData = res.data.ncov_data.areaTree[0].children;
        const areaDetail = [];
        for (let i = 0; i < _areaData.length; i++) {
          areaDetail.push({
            city:_areaData[i].name,
            quezhen:_areaData[i].total.confirm,
            add:_areaData[i].today.confirm
          })
        }
        var areaTree = res.data.ncov_data.areaTree;
        var chinaData = areaTree.shift();
        const overseaData = areaTree;
        that.setData({
          addDataTrend: addDataTrend,
          allDataTrend: allDataTrend,
          allCureTrend: allCureTrend,
          areaDetail:areaDetail,
          overseaData: overseaData
        });
      }
    });
  }
})